﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Collections;
using mshtml;
using System.Windows.Forms;
using System.Xml;
using System.Web.UI.HtmlControls;



namespace WindowsFormsApplication1
{    
    public class HTMLForm
    {
        string _method = "";
        string _action = "";
        public Dictionary<string, string> Inputs = new Dictionary<string, string>();

        public string Method { get { return _method; } set { _method = value; } }

        public string Action { get { return _action; } set { _action = value; } }

        
        #region OldMethods
        public HTMLForm(IHTMLFormElement element)
        {

            _method = element.method;
            _action = element.action;

            IHTMLElementCollection elements = (IHTMLElementCollection)element.tags("input");

            foreach (IHTMLInputElement x in elements)
            {
                try
                {
                    string name = x.name;
                    string value = x.value;
                    if (name == null)
                        name = x.type;

                    Inputs.Add(name, value);
                }
                catch { }
            }

        }

        public string ToPostData()
        {
            StringBuilder sb = new StringBuilder();
            foreach (string key in Inputs.Keys)
            {
                object x = Inputs[key];
                string value = (x == null) ? "" : x.ToString();
                sb.AppendFormat("{0}={1}&", MyHttpLoginBase.Encode(key), MyHttpLoginBase.Encode(value));
            }
            if (sb.Length > 1)
                return sb.ToString().Substring(0, sb.Length - 1);
            else
                return "";

        }

        public static HTMLForm[] GetAllForms(string html)
        {
            //TextReader tr
            XmlDocument xDoc = new XmlDocument();
            //xDoc.Load(

            List<HTMLForm> forms = new List<HTMLForm>();
            
            IHTMLDocument2 doc = new HTMLDocumentClass();
            doc.write(new object[] { html });
            doc.close();

            HTMLDocumentClass doc2 = doc as HTMLDocumentClass;
            IHTMLElementCollection elements = doc.forms;
            
            foreach (IHTMLFormElement element in elements)
            {                
                forms.Add(new HTMLForm(element));
            }
            return forms.ToArray();
        }

        public static HTMLForm GetFormByAction(string html, string action)
        {
            HTMLForm[] forms = HTMLForm.GetAllForms(html);
            foreach (HTMLForm form in forms)
                if (form.Action.ToLower() == action.ToLower())
                    return form;
            return null;
        }
        #endregion

        #region NewMyMethods
        //public static HTMLForm[] GetAllForms2(Stream html)
        //{
        //    //TextReader tr
        //    XmlDocument xDoc = new XmlDocument();
        //    xDoc.Load(html);
        //    //xDoc.Value

        //    HtmlDocument ht = new HtmlDocument();
        //    ht.Write

        //    List<HTMLForm> forms = new List<HTMLForm>();
            
        //    IHTMLDocument2 doc = new HTMLDocumentClass();
        //    doc.write(new object[] { html });
        //    doc.close();

        //    HTMLDocumentClass doc2 = doc as HTMLDocumentClass;
        //    IHTMLElementCollection elements = doc.forms;
            
        //    foreach (IHTMLFormElement element in elements)
        //    {                
        //        forms.Add(new HTMLForm(element));
        //    }
        //    return forms.ToArray();
        //}        
        #endregion

    }    
}
